import nativeFetch from '@system.fetch';
import prompt from '@system.prompt';
const BASE_URL = ``;
class http {
    static request(method, Aurl, data, success, fail) {
        let url = Aurl;
        const strRegex = '(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]';
        var reg = new RegExp(strRegex);
        if (!reg.test(url)) {
            url = BASE_URL + url;
        }
        console.warn(`请求地址:${url}`);
        this.fetch(method, url, data, success, fail)

    }
    static fetch(method, url, data, success, fail) {
        nativeFetch.fetch({
            method,
            url,
            data,
            success: success,
            fail: fail
        })
    }
    static isSuccess(res) {
        const quickappCode = res.code;
        // 快应用请求错误
        if (quickappCode !== 200) {
            return false;
        }
        const quickappData = JSON.parse(res.data)
        return !(quickappData && quickappData.status !== 0);
    }
    /**
     * 异常
     */
    static requestException(res) {
        const error = {};
        error.statusCode = res.code;
        const quickappData = res.data;
        if (quickappData) {
            const serverData = JSON.parse(res.data)
            if (serverData) {
                error.serverCode = serverData.status;
                error.message = serverData.msg;
                error.serverData = serverData.data;
            }
        }
        return error;
    }
    static get(url, data) {
        return this.request('GET', url, data, success, fail);
    }

    static put(url, data) {
        return this.request('PUT', url, data, success, fail);
    }

    static post(url, data) {
        return this.request('POST', url, data, success, fail);
    }

    static patch(url, data) {
        return this.request('PATCH', url, data, success, fail);
    }

    static delete(url, data) {
        return this.request('DELETE', url, data, success, fail);
    }
}
// 全局注册
// export default base
const injectRef = Object.getPrototypeOf(global) || global
injectRef.$http = http

export default {
    http: http
}